<?php
namespace PsiFramework\Classes\Factory;

import("Classes.Util.Manager.ApplicationManager");

use \PsiFramework\Classes\Util\Manager\ApplicationManager;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */


/**
 * The DAOFactory is used to get the correct DAO instance
 *
 * @package Factories
 */
class DAOFactory
{

    /**
     * Static function to retrieve the correct DAO based on the object name
     *
     * @param string $objectName the object name
     * 
     * @return \PsiFramework\Classes\DAO\AbstractDAO
     */
    public static function getDAO(/*string*/ $objectName)
    {

        $applicationManager = ApplicationManager::getInstance();
        $application = $applicationManager->getApplication();

        $objectParameters = $application->getObjectParameter($objectName);
        if (is_null($objectParameters)) {
            return null ;
        }

        $dao = $objectParameters->getDAO();
        if ($dao == null) {
            return null ;
        }

        import($dao);
        $dao = getClassName($dao);

        if (!class_exists($dao)) {
            return null;
        }
        $instance = call_user_func(
            array($dao, 'getInstance'),
            $objectParameters
        );

        return $instance ;
    }
}
